package net.dipper.tankwar.editor.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import wnb.util.config.ConfigurationManager;

import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.config.ConfigItem;

public class EditorPane extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ObjectPane objPane;
	private MapPane mapPane;
	private OperationPane operationPane;
	private EditorContext context;
	private int MAP_WIDTH = 50;
	private int MAP_HEIGHT = 50;
	
	public EditorPane() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException{
		this.context = new EditorContext(new ObjectMap(MAP_WIDTH, MAP_HEIGHT));
		
		int delay = Integer.parseInt(ConfigurationManager.getAppSetting("mapDelay"));
		this.objPane = new ObjectPane();
		this.mapPane = new MapPane(context, delay, true);
		this.operationPane = new OperationPane(context);
		
		this.objPane.addActionListener(this);
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		add(new JScrollPane(this.objPane));
		add(Box.createRigidArea(new Dimension(10, 10)));
		add(this.mapPane);
		add(Box.createRigidArea(new Dimension(10, 10)));
		add(this.operationPane);
		
		this.mapPane.start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source instanceof ConfigItem){
			context.setCurrentItem((ConfigItem)source);
			context.setMode(EditorContext.MODE_EDIT);
		}
	}
}
